class Product < ActiveRecord::Base
  validates_uniqueness_of :cn_name,:allow_blank => true,:message => "此产品已经存在!"
  
  before_save :update_total
  
  belongs_to :category
  has_many :productusers,:include => ["user"]
  has_many :users ,:through => :productusers,:order => "last_name ASC",:include => ["department"]
  has_many :inproducts
  
  # def before_save
  #   self.remaining = self.total_pre + self.total_in + self.total_out
  #   self.save!
  # end
  
  def name_with_remaining
    "#{cn_name}--库存:#{remaining}--单价:#{per_cost}--"
    
  end
  
  def order_num
    ""
  end
  
  def description
    ""
  end
  
  def total_price
    0
  end
  
  def update_total
      self.total_amount = self.total_pre + self.total_in
      self.remaining = self.total_amount + self.total_out
      # save
  end
  
end
